#Boa:Frame:requestFrame

import wx
import logging
from request import *

#get the logger instance for this app
logger = logging.getLogger('provinci')

def create(parent):
    return requestFrame(parent)

[wxID_REQUESTFRAME, wxID_REQUESTFRAMEADDBUTTON, 
 wxID_REQUESTFRAMEADDRESSTEXTCTRL, wxID_REQUESTFRAMECANCELBUTTON, 
 wxID_REQUESTFRAMECOMMENTTEXTCTRL, wxID_REQUESTFRAMEDESCRTEXTCTRL, 
 wxID_REQUESTFRAMEEDITBUTTON, wxID_REQUESTFRAMEFAMILYNAMETEXTCTRL, 
 wxID_REQUESTFRAMEFILESTATICBOX, wxID_REQUESTFRAMEGIVENTEXTCTRL, 
 wxID_REQUESTFRAMELISTBOX, wxID_REQUESTFRAMEOKBUTTON, wxID_REQUESTFRAMEPANEL1, 
 wxID_REQUESTFRAMEREMOVEBUTTON, wxID_REQUESTFRAMESTATICTEXT1, 
 wxID_REQUESTFRAMESTATICTEXT2, wxID_REQUESTFRAMESTATICTEXT3, 
 wxID_REQUESTFRAMESTATICTEXT4, wxID_REQUESTFRAMESTATICTEXT5, 
 wxID_REQUESTFRAMEYOUSTATICBOX, 
] = [wx.NewId() for _init_ctrls in range(20)]

class requestFrame(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_REQUESTFRAME, name=u'requestFrame',
              parent=prnt, pos=wx.Point(685, 201), size=wx.Size(593, 785),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Request')
        self.SetClientSize(wx.Size(585, 758))

        self.panel1 = wx.Panel(id=wxID_REQUESTFRAMEPANEL1, name='panel1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(585, 758),
              style=wx.TAB_TRAVERSAL)

        self.staticText1 = wx.StaticText(id=wxID_REQUESTFRAMESTATICTEXT1,
              label=u'Comment', name='staticText1', parent=self.panel1,
              pos=wx.Point(48, 456), size=wx.Size(45, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_REQUESTFRAMESTATICTEXT2,
              label=u'Address', name='staticText2', parent=self.panel1,
              pos=wx.Point(48, 120), size=wx.Size(39, 13), style=0)

        self.staticText4 = wx.StaticText(id=wxID_REQUESTFRAMESTATICTEXT4,
              label=u'Family name', name='staticText4', parent=self.panel1,
              pos=wx.Point(48, 88), size=wx.Size(59, 13), style=0)

        self.staticText3 = wx.StaticText(id=wxID_REQUESTFRAMESTATICTEXT3,
              label=u'Given name', name='staticText3', parent=self.panel1,
              pos=wx.Point(48, 64), size=wx.Size(56, 13), style=0)

        self.givenTextCtrl = wx.TextCtrl(id=wxID_REQUESTFRAMEGIVENTEXTCTRL,
              name=u'givenTextCtrl', parent=self.panel1, pos=wx.Point(120, 56),
              size=wx.Size(400, 21), style=0, value=u'')

        self.familyNameTextCtrl = wx.TextCtrl(id=wxID_REQUESTFRAMEFAMILYNAMETEXTCTRL,
              name=u'familyNameTextCtrl', parent=self.panel1, pos=wx.Point(120,
              88), size=wx.Size(399, 21), style=0, value=u'')

        self.addressTextCtrl = wx.TextCtrl(id=wxID_REQUESTFRAMEADDRESSTEXTCTRL,
              name=u'addressTextCtrl', parent=self.panel1, pos=wx.Point(120,
              120), size=wx.Size(400, 104), style=wx.TE_MULTILINE, value=u'')

        self.staticText5 = wx.StaticText(id=wxID_REQUESTFRAMESTATICTEXT5,
              label=u'Description', name='staticText5', parent=self.panel1,
              pos=wx.Point(48, 416), size=wx.Size(53, 13), style=0)

        self.descrTextCtrl = wx.TextCtrl(id=wxID_REQUESTFRAMEDESCRTEXTCTRL,
              name=u'descrTextCtrl', parent=self.panel1, pos=wx.Point(120, 416),
              size=wx.Size(400, 21), style=0, value=u'')

        self.commentTextCtrl = wx.TextCtrl(id=wxID_REQUESTFRAMECOMMENTTEXTCTRL,
              name=u'commentTextCtrl', parent=self.panel1, pos=wx.Point(120,
              448), size=wx.Size(400, 152), style=wx.TE_MULTILINE, value=u'')

        self.addButton = wx.Button(id=wxID_REQUESTFRAMEADDBUTTON, label=u'add',
              name=u'addButton', parent=self.panel1, pos=wx.Point(448, 616),
              size=wx.Size(75, 23), style=0)
        self.addButton.Bind(wx.EVT_LEFT_UP, self.OnAddButtonLeftUp)

        self.editButton = wx.Button(id=wxID_REQUESTFRAMEEDITBUTTON,
              label=u'Edit', name=u'editButton', parent=self.panel1,
              pos=wx.Point(120, 352), size=wx.Size(75, 23), style=0)
        self.editButton.Bind(wx.EVT_LEFT_UP, self.OnEditButtonLeftUp)

        self.removeButton = wx.Button(id=wxID_REQUESTFRAMEREMOVEBUTTON,
              label=u'Remove', name=u'removeButton', parent=self.panel1,
              pos=wx.Point(448, 352), size=wx.Size(75, 23), style=0)
        self.removeButton.Bind(wx.EVT_LEFT_UP, self.OnRemoveButtonLeftUp)

        self.fileStaticBox = wx.StaticBox(id=wxID_REQUESTFRAMEFILESTATICBOX,
              label=u'Fileset', name=u'fileStaticBox', parent=self.panel1,
              pos=wx.Point(40, 392), size=wx.Size(496, 264), style=0)

        self.youStaticBox = wx.StaticBox(id=wxID_REQUESTFRAMEYOUSTATICBOX,
              label=u'You', name=u'youStaticBox', parent=self.panel1,
              pos=wx.Point(40, 32), size=wx.Size(496, 200), style=0)

        self.okButton = wx.Button(id=wxID_REQUESTFRAMEOKBUTTON, label=u'OK',
              name=u'okButton', parent=self.panel1, pos=wx.Point(200, 696),
              size=wx.Size(75, 23), style=0)
        self.okButton.Bind(wx.EVT_LEFT_UP, self.OnOkButtonLeftUp)

        self.cancelButton = wx.Button(id=wxID_REQUESTFRAMECANCELBUTTON,
              label=u'Cancel', name=u'cancelButton', parent=self.panel1,
              pos=wx.Point(304, 696), size=wx.Size(75, 23), style=0)
        self.cancelButton.Bind(wx.EVT_LEFT_UP, self.OnCancelButtonLeftUp)

        self.listBox = wx.ListBox(choices=[], id=wxID_REQUESTFRAMELISTBOX,
              name=u'listBox', parent=self.panel1, pos=wx.Point(120, 248),
              size=wx.Size(400, 96), style=wx.VSCROLL | wx.LB_SINGLE)
        self.listBox.SetLabel(u'File sets')
        self.listBox.SetSelection(-1)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.filesets = []
        self.parent = parent

    def addFileSet(self, fileset):
        self.filesets.append(fileset)
        self.listBox.SetItems([str(fileset) for fileset in self.filesets])

    def removeFileSet(self, index):
        del self.filesets[index]
        self.listBox.SetItems([fileset.description for fileset in self.filesets])
        

    #---Event Handlers----------------------------------------------------------------------------

    def OnOkButtonLeftUp(self, event):
        requester = Requester(self.givenTextCtrl.Value, self.familyNameTextCtrl.Value, self.addressTextCtrl.Value)
        request = Request(requester)
        request.filesets = self.filesets
        self.parent.sendRequest(request)
        self.Close()
        event.Skip()

    def OnCancelButtonLeftUp(self, event):
        self.Close()
        event.Skip()

    def OnAddButtonLeftUp(self, event):   
        fileset = FileSet(self.descrTextCtrl.Value, self.commentTextCtrl.Value)
        self.descrTextCtrl.Clear()
        self.commentTextCtrl.Clear()
        self.addFileSet(fileset)
        event.Skip()       

    def OnEditButtonLeftUp(self, event):
        index = self.listBox.GetSelection()
        fileset = self.filesets[index]        
        self.descrTextCtrl.Value = fileset.description
        self.commentTextCtrl.Value = fileset.comment
        self.removeFileSet(index)
        event.Skip()

    def OnRemoveButtonLeftUp(self, event):
        selection = self.listBox.GetSelection()
        if selection >= 0:
            self.listBox.Delete(selection)
        event.Skip()
